package com.eastfair.builder.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.builder.dao.ReportDrawingCategoryMapper;
import com.eastfair.builder.dto.ReportDrawingCategoryDTO;
import com.eastfair.builder.entity.ReportDrawingCategory;
import com.eastfair.builder.service.ReportDrawingCategoryService;
import com.eastfair.builder.vo.ReportDrawingCategoryVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 报馆图纸分类
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportDrawingCategoryServiceImpl extends SuperServiceImpl<ReportDrawingCategoryMapper, ReportDrawingCategory> implements ReportDrawingCategoryService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportDrawingCategory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportDrawingCategory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportDrawingCategoryVO> listReportDrawingCategoryVOsByReportSpaceId(Long reportSpaceId) {
        log.info("listReportDrawingCategoryVOsByReportSpaceId - 报馆ID查询图纸分类列表, reportSpaceId={}", reportSpaceId);
        ReportDrawingCategoryDTO query = new ReportDrawingCategoryDTO();
        query.setReportSpaceId(reportSpaceId);
        List<ReportDrawingCategory> list = listReportDrawingCategories(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list.stream()
                .map(reportDrawingCategory -> BeanUtil.toBean(reportDrawingCategory, ReportDrawingCategoryVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportDrawingCategory> listReportDrawingCategories(ReportDrawingCategoryDTO query) {
        log.info("listReportDrawingCategories - 查询报馆图纸分类列表, query={}", query);
        QueryWrapper<ReportDrawingCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ReportDrawingCategory::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportSpaceId() != null, ReportDrawingCategory::getReportSpaceId, query.getDrawingCategoryId())
        ;
        return list(queryWrapper);
    }

    @Override
    public boolean saveReportDrawingCategory(ReportDrawingCategoryDTO categoryDTO) {
        return false;
    }
}
